package sa.com.sure.ejtimaa.database;


import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import sa.com.sure.ejtimaa.common.AppHelper;
import sa.com.sure.ejtimaa.common.Helper;
import sa.com.sure.ejtimaa.model.MeetingInfo;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;


public class Meetings {

	private DataBaseHelper dbHelper;
	
	public static final String TABLE_Name = "Meetings";
	
    public static final String MeetingIDField = "MeetingID";
    public static final String SessionIDField = "SessionID";    
    public static final String MeetingStatusIDField = "MeetingStatusID";
    public static final String MeetingPlaceIDField = "MeetingPlaceID";
    public static final String TitleField = "Title";
    public static final String StartDateField = "StartDate";
    public static final String EndDateField = "EndDate";
    public static final String MeetingNoField = "MeetingNo";
    public static final String MeetingBriefField = "MeetingBrief";
    public static final String WaitingForAttendeesReplyField = "WaitingForAttendeesReply";
    public static final String MeetingStatusDateTimeField = "MeetingStatusDateTime";
    public static final String CreatedDateTimeField = "CreatedDateTime";
    public static final String UpdatedDateTimeField = "UpdatedDateTime";
    
    private String[] allColumns = {  
			    		MeetingIDField,
			    		SessionIDField,
			    		MeetingStatusIDField,
			    		MeetingPlaceIDField,
			    		TitleField,
			    		StartDateField,
			    		EndDateField,
			    		MeetingNoField,
			    		MeetingBriefField,
			    		WaitingForAttendeesReplyField,
			    		MeetingStatusDateTimeField,
			    		CreatedDateTimeField,
			    		UpdatedDateTimeField};
    
    Cursor cursor = null;
    
    public Meetings(Context context)  {
 		dbHelper = new DataBaseHelper(context);
 		dbHelper.createDataBase();
 	}
        
    public static Meetings Instance = new Meetings(AppHelper.getAppContext());
    
	public void open() throws SQLException {
		dbHelper.openDataBase();
	}
	
	public void close() {
		if(cursor!=null && !cursor.isClosed())
			cursor.close();
		dbHelper.close();
	}
	
	public void addItem(MeetingInfo item) throws ParseException { 
        
		open();
		
        ContentValues values = new ContentValues(); 
        
        values.put(MeetingIDField, item.GetMeetingID());
        values.put(SessionIDField, item.GetSessionID());
        values.put(MeetingStatusIDField, item.GetMeetingStatusID());
        values.put(MeetingPlaceIDField, item.GetMeetingPlaceID());
        values.put(TitleField, item.GetTitle());
        values.put(StartDateField, Helper.GetDateAsStringFormat(item.GetStartDate(),Helper.DateFormatWebService));
        values.put(EndDateField, Helper.GetDateAsStringFormat(item.GetEndDate(),Helper.DateFormatWebService));
        values.put(MeetingNoField, item.GetMeetingNo());
        values.put(MeetingBriefField, item.GetMeetingBrief());
        values.put(WaitingForAttendeesReplyField, item.GetWaitingForAttendeesReply());
        values.put(MeetingStatusDateTimeField, Helper.GetDateAsStringFormat(item.GetMeetingStatusDateTime(),Helper.DateFormatWebService));
        values.put(CreatedDateTimeField, Helper.GetDateAsStringFormat(item.GetCreatedDateTime(),Helper.DateFormatWebService));
        values.put(UpdatedDateTimeField, Helper.GetDateAsStringFormat(item.GetUpdatedDateTime(),Helper.DateFormatWebService));        
        
        
        // Inserting Row 
        dbHelper.db.insert(TABLE_Name, null, values); 
        
        close();
    } 
	
	
	public MeetingInfo GetItem(int MeetingID) throws SQLException, ParseException{
		
		open();
	
		MeetingInfo item = null;
		
		    cursor = dbHelper.db.query(TABLE_Name,allColumns, MeetingIDField + "=" + MeetingID,null,null,null,null);
        	if (cursor != null && cursor.getCount()>0) {
	        	cursor.moveToFirst();	        	
        	item =  getItemInfo(cursor);
	     
        }	        
        
        close();
        
    	return 	item;	        
	    
	}
	
	public List<MeetingInfo> GetAllItems() throws SQLException, ParseException{
		
		open();
	
		List<MeetingInfo> items = new ArrayList<MeetingInfo>();
		
		cursor = dbHelper.db.query( TABLE_Name,allColumns, null,null,null,null,null);
        		        	 
		if (cursor.moveToFirst()){
		   do{
			   items.add(getItemInfo(cursor));
		   }while(cursor.moveToNext());
		}
        		        		                     
        close();
        
    	return 	items;	        
	    
	}
	
	
	 public boolean updateItem(MeetingInfo item) throws ParseException {
		 
	 	open();
	 	
	    ContentValues values = new ContentValues();		   
	    
        values.put(SessionIDField, item.GetSessionID());
        values.put(MeetingStatusIDField, item.GetMeetingStatusID());        
        values.put(MeetingPlaceIDField, item.GetMeetingPlaceID());
        values.put(TitleField, item.GetTitle());
        values.put(StartDateField, Helper.GetDateAsStringFormat(item.GetStartDate(),Helper.DateFormatWebService));
        values.put(EndDateField, Helper.GetDateAsStringFormat(item.GetEndDate(),Helper.DateFormatWebService));
        values.put(MeetingNoField, item.GetMeetingNo());
        values.put(MeetingBriefField, item.GetMeetingBrief());
        values.put(WaitingForAttendeesReplyField, item.GetWaitingForAttendeesReply());
        values.put(MeetingStatusDateTimeField, Helper.GetDateAsStringFormat(item.GetMeetingStatusDateTime(),Helper.DateFormatWebService));
        values.put(CreatedDateTimeField, Helper.GetDateAsStringFormat(item.GetCreatedDateTime(),Helper.DateFormatWebService));
        values.put(UpdatedDateTimeField, Helper.GetDateAsStringFormat(item.GetUpdatedDateTime(),Helper.DateFormatWebService)); 	    
	    boolean res =  dbHelper.db.update(TABLE_Name, values, MeetingIDField + "=" + item.GetMeetingID(), null) > 0;
	    
	    close();
	    
	    return res;
	 }
	
	
    private MeetingInfo getItemInfo(Cursor cursor) throws ParseException {  
    	
    	MeetingInfo itemInfo = new MeetingInfo();    
    	
    	itemInfo.SetMeetingID(cursor.getInt(cursor.getColumnIndex(MeetingIDField)));
    	itemInfo.SetSessionID(cursor.getInt(cursor.getColumnIndex(SessionIDField)));
    	itemInfo.SetMeetingStatusID(cursor.getInt(cursor.getColumnIndex(MeetingStatusIDField)));
    	itemInfo.SetMeetingPlaceID(cursor.getInt(cursor.getColumnIndex(MeetingPlaceIDField)));
    	itemInfo.SetTitle(cursor.getString(cursor.getColumnIndex(TitleField)));
    	itemInfo.SetStartDate(Helper.GetDateFormat(cursor.getString(cursor.getColumnIndex(StartDateField)),Helper.DateFormatWebService));
    	itemInfo.SetEndDate(Helper.GetDateFormat(cursor.getString(cursor.getColumnIndex(EndDateField)),Helper.DateFormatWebService));
    	itemInfo.SetMeetingNo(cursor.getString(cursor.getColumnIndex(MeetingNoField)));
    	itemInfo.SetMeetingBrief(cursor.getString(cursor.getColumnIndex(MeetingBriefField)));
    	itemInfo.SetWaitingForAttendeesReply(cursor.getInt(cursor.getColumnIndex(WaitingForAttendeesReplyField)));
    	itemInfo.SetMeetingStatusDateTime(Helper.GetDateFormat(cursor.getString(cursor.getColumnIndex(MeetingStatusDateTimeField)),Helper.DateFormatWebService));
    	itemInfo.SetCreatedDateTime(Helper.GetDateFormat(cursor.getString(cursor.getColumnIndex(CreatedDateTimeField)),Helper.DateFormatWebService));
    	itemInfo.SetUpdatedDateTime(Helper.GetDateFormat(cursor.getString(cursor.getColumnIndex(UpdatedDateTimeField)),Helper.DateFormatWebService));

    	
		return itemInfo;
	}
}
